package com.sharing.service;

import com.sharing.common.Page;
import com.sharing.common.Result;
import com.sharing.domain.Hobby;
import com.sharing.domain.User;
import com.sharing.domain.UserHobby;
import com.sharing.mapper.HobbyMapper;
import com.sharing.mapper.UserHobbyMapper;
import com.sharing.mapper.UserMapper;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.StyledEditorKit;
import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author MingTian
 * @version 1.0
 * @description TODO
 * @date 2023/4/3 10:39
 */
@Service
public class HobbyService {
    @Autowired
    private HobbyMapper hobbyMapper;

    /**
     * 查询一级兴趣的业务层方法
     * @param page
     * @param size
     * @return
     */
    public Result<Page<Hobby>> selectFirstLevelHobby(Integer page, Integer size) {
        // 校验参数
        if (page == null || size == null) {
            return new Result<>(1, "参数不当");
        }

        // 查询一级参数的总数
        Integer total = hobbyMapper.selectFirstLevelHobbyNum();

        // 按照分页条件查询特定的一级兴趣
        List<Hobby> hobbies = hobbyMapper.selectFirstLevelHobby((page - 1) * size, size);

        return new Result<>(0, new Page<>(total, size, hobbies));
    }

    /**
     * 根据一级兴趣id查询对应的二级兴趣的业务层方法
     * @param parentId
     * @param page
     * @param size
     * @return
     */
    public Result<Page<Hobby>> selectSecondLevelHobbyByParentId(Integer parentId, Integer page, Integer size) {
        // 校验参数一：校验page、size分页参数
        if (page == null || size == null) {
            return new Result<>(1, "参数不当");
        }

        // 校验参数二：校验parentId是否存在
        List<Integer> ids = new ArrayList<>();
        ids.add(parentId);
        List<Hobby> hobby = hobbyMapper.selectHobbyByIds(ids);
        if (ids == null || ids.size() != 1 || ids.get(0) == null) {
            return new Result<>(1, "parentId不存在或者已删除");
        }

        // 查询该一级兴趣下二级兴趣的总数
        Integer total = hobbyMapper.selectSecondLevelHobbyNumByParentId(parentId);

        // 按照分页条件查询该一级兴趣的二级兴趣
        List<Hobby> hobbies = hobbyMapper.selectSecondLevelHobbyByParentId(parentId, (page - 1) * size, size);

        return new Result<>(0, new Page<>(total, size, hobbies));
    }

    /**
     * 新增一级兴趣的业务层方法
     * @param hobby
     * @return
     */
    public Result<Hobby> insertFirstLevelHobby(Hobby hobby) {
        // 校验参数
        if (hobby == null || hobby.getHobby() == null) {
            return new Result<>(1, "参数不当");
        }

        // 根据参数的hobby参数查询数据库中是否已经存在该兴趣
        Hobby hobby1 = hobbyMapper.selectLevelHobbyByHobby(hobby.getHobby());
        if (hobby1 != null) {
            return new Result<>(1, "该兴趣已经存在，请不要重复添加");
        }

        hobby.setParentId(0);
        hobby.setLevel(1);
        Integer rows = hobbyMapper.insertHobby(hobby);

        if (rows != 1) {
            return new Result<>(1, "新增失败");
        }

        return new Result<>(0, hobby);
    }

    /**
     * 新增二级兴趣
     * @param hobby
     * @return
     */
    public Result<Hobby> insertSecondLevelHobby(Hobby hobby) {
        // 校验参数
        if (hobby == null || hobby.getParentId() == null || hobby.getHobby() == null) {
            return new Result<>(1, "参数不当");
        }

        List<Integer> ids = new ArrayList<>();
        ids.add(hobby.getParentId());
        List<Hobby> hobby1 = hobbyMapper.selectHobbyByIds(ids);

        if (hobby1 == null || hobby1.size() != 1 || hobby1.get(0) == null) {
            return new Result<>(1, "参数不当");
        }

        hobby.setLevel(2);
        Integer rows = hobbyMapper.insertHobby(hobby);

        if (rows != 1) {
            return new Result<>(1, "新增失败");
        }

        return new Result<>(0, hobby);
    }

    /**
     * 删除一级兴趣的业务层方法
     * @param ids
     * @return
     */
    public Result<Integer> deleteFirstLevelHobby(List<Integer> ids) {
        // 校验参数
        if (ids == null || ids.size() == 0) {
            return new Result<>(1, "参数不当");
        }

        // 一级兴趣删除需所有一级兴趣均无子兴趣
        boolean isHasChildHobby = false;
        for (Integer id : ids) {
            // 查询该一级兴趣下二级兴趣的总数
            Integer total = hobbyMapper.selectSecondLevelHobbyNumByParentId(id);
            if (total > 0) {
                return new Result<>(1, "存在所选中的一级兴趣有二级兴趣");
            }
        }

        Integer rows = hobbyMapper.deleteHobby(ids);

        if (rows < ids.size()) {
            return new Result<>(1, (ids.size() - rows) + "个兴趣删除失败");
        }

        return new Result<>(0, rows);
    }

    /**
     * 删除二级兴趣的业务层方法
     * @param ids
     * @return
     */
    public Result<Integer> deleteSecondLevelHobby(List<Integer> ids) {
        // 校验参数
        if (ids == null || ids.size() == 0) {
            return new Result<>(1, "参数不当");
        }

        Integer rows = hobbyMapper.deleteHobby(ids);

        if (rows < ids.size()) {
            return new Result<>(1, (ids.size() - rows) + "个兴趣删除失败");
        }

        return new Result<>(0, rows);
    }

    /**
     * 根据兴趣id查询兴趣
     * @param id
     * @return
     */
    public String selectHobbyByHobbyId(Integer id) {
        return hobbyMapper.selectHobbyByHobbyId(id);
    }
}
